package com.jt.config;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.SourceLocation;
import org.springframework.stereotype.Component;

import javax.sound.midi.Soundbank;
import java.util.Arrays;

//@Aspect
//@Component
public class MyAspects {

    //抽取公共的切入点表达式
    //1、本类引用
    //2、其他的切面引用
    @Pointcut("execution(public  *  com.jt.service.UserServiceImpl.*(..))")
    public void pointCut(){};

    //@Before在目标方法之前切入；切入点表达式（指定在哪个方法切入）
    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println("--------->"+joinPoint.getSignature().getName()+":前置通知");
    }

    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint){
        System.out.println("--------->"+joinPoint.getSignature().getName()+":后置通知");
    }

    //JoinPoint一定要出现在参数表的第一位
    @AfterReturning(value="pointCut()",returning="result")
    public void logReturn(JoinPoint joinPoint,Object result){
        System.out.println("--------->"+joinPoint.getSignature().getName()+":返回通知");
    }

    @AfterThrowing(value="pointCut()",throwing="exception")
    public void logException(JoinPoint joinPoint,Exception exception){
        System.out.println("--------->"+joinPoint.getSignature().getName()+":异常：{"+exception+"}");
    }

    @Around("pointCut()")
    public Object  round(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("--------->"+joinPoint.getSignature().getName()+":around环绕的切入");
        Object result = joinPoint.proceed(joinPoint.getArgs());
        return  result;
    }

}
